<?php
require_once 'Util.php';
class Db extends Util
{
    protected $_host = 'localhost';
    protected $_dbname = '';
    protected $_uname = '';
    protected $_port = '';
    protected $_psw = '';
    protected $_prefix = '';
    protected $_linkid = null;
    protected $_sql = '';
    /**
     * 初始化类
     *
     * @param string $host 主机
     * @param string $uname 用户名
     * @param string $psw 密码
     * @param string $port 端口号
     * @param string $prefix 表前缀
     */
    function __construct($host='',$uname='',$psw='',$dbname='',$port='',$prefix='')
    {
        $this->_host = $host ? $host : DB_HOST;
        $this->_uname = $uname ? $uname : DB_UNAME;
        $this->_dbname = $dbname ? $dbname : DB_NAME;
        $this->_psw = $psw ? $psw : DB_PSW;
        $this->_port = $port ? $port : DB_PORT;
        $this->_prefix = $prefix ? $prefix : DB_PREFIX; 
    }
    
    /**
     * 执行sql
     *
     * @param string $sql
     * @param string $reType='id'|'obj'|'affect'
     * @return 查询结果
     */
    public function query($sql,$reType='obj')
    {
        $reTypeAry = array(
            'id','obj','affect'
        );
        $this->_connect();
        $this->_sql = $this->_replaceSql($sql);
        
        $result = mysql_query($this->_sql);
        if($result){
            if($reType == 'obj'){
                return $result;
            }elseif($reType == 'id'){
                return mysql_insert_id( $this->_linkid );
            }else{
                return mysql_affected_rows( $this->_linkid );
            }
        }else{
            $this->_errorMsg = mysql_error();
            return false;
        }
        $this->_close();
    }
    
    /**
     * 查询一条记录
     *
     * @param string $sql
     * @return ary
     */
    public function fetchRow($sql)
    {
        $result = $this->fetch( $sql );
        return $result ? $result[0] : array();
    }
    
    /**
     * 查询记录集
     *
     * @param string $sql
     * @return array
     */
    public function fetch($sql)
    {
        $result = array();
        $ret = $this->query($sql);
        $row = array();
        if( $ret ){
            while ( $row = mysql_fetch_assoc($ret) ) {
            	$result[] = $row;
            }
        }
        return $result;
    }
    
    /**
     * 创建数据库连接
     *
     */
    private function _connect()
    {
        $id = mysql_connect($this->_host,$this->_uname,$this->_psw);
        if($id){
            $this->_linkid = $id;
            mysql_select_db( $this->_dbname , $this->_linkid );
        }else{
            $this->_errorMsg = mysql_error($this->_linkid);
        }
    }
    
    /**
     * 返回最后一条SQL语句
     *
     * @return string
     */
    public function getLastSql()
    {
        return $this->_sql;
    }
    
    /**
     * 构建插入语句
     *
     * @param array $row
     * @param string $table
     * @return string
     */
    public function buildInsertSql($row,$table)
    {
        $fields = '';
        $values = '';
        $sql = '';
        if( is_array($row) ){
            foreach ($row as $key=>$value){
                $fields .= "`{$key}`,";
                $values .= "'{$value}',";
            }
            $fields = substr( $fields , 0 , strlen($fields)-1 );
            $values = substr( $values , 0 , strlen($values)-1 );
            $sql = "INSERT INTO `#@__{$table}`({$fields})VALUES({$values})";
            return $sql;
        }else{
            return false;
        }
    }
    
    /**
     * 构建更新语句
     *
     * @param array $row
     * @param string $table
     * @param string $where
     * @return string
     */
    public function buildUpdateSql($row,$table,$where)
    {
    	$sql = '';
        if( is_array($row) ){
            foreach ($row as $key=>$value){
                $sql .= "`{$key}`='{$value}',";
            }
            $sql = substr( $sql , 0 , strlen($sql)-1 );
            $sql = "UPDATE `#@__{$table}` SET {$sql} WHERE {$where}";
            return $sql;
        }else{
            return false;
        }
    }
    
    /**
     * 关闭与数据库的连接
     *
     */
    private function _close()
    {
        mysql_close($this->_linkid);
    }
    
    /**
     * 处理SQL语句中的表前缀符
     *
     * @param String $sql
     * @return string
     */
    private function _replaceSql($sql)
    {
        return str_replace( '#@__' , DB_PREFIX , $sql );
    }
}